class Player:
    def __init__(self, name, game_map=None, room_name=None):
        # 基本属性
        self.name = name
        self.level = 1
        self.exp = 0
        self.hp = 100
        self.max_hp = 100
        self.mp = 100
        self.max_mp = 100
        
        # 基础属性
        self.strength = 10      # 力量
        self.agility = 10      # 敏捷
        self.intelligence = 10  # 智力
        self.vitality = 10     # 体力
        
        # 位置信息
        self.current_map = game_map
        if game_map and room_name:
            # 如果提供了地图和房间名，设置玩家位置
            for pos, room in game_map.room_positions.items():
                if room == room_name:
                    self.x, self.y = pos
                    break
        else:
            # 如果没有提供地图和房间，使用默认位置
            self.x = 0
            self.y = 0
        
        # 背包
        self.inventory = []
        self.equipment = {
            'weapon': None,
            'armor': None,
            'accessory': None
        }
        
        # 技能列表
        self.skills = []
        
    def go(self, command):
        """处理带go的移动命令
        
        Args:
            command: 移动命令，例如 "go n", "go south" 等
            
        Returns:
            bool: 移动是否成功
        """
        # 移除开头的 "go" 并去除多余空格
        if command.lower().startswith("go "):
            direction = command[3:].strip()
            return self.move(direction)
        return self.move(command)
        
    def move(self, direction):
        """移动玩家位置
        
        Args:
            direction: 移动方向 (北/南/东/西 或 n/s/e/w)
            
        Returns:
            bool: 移动是否成功
        """
        # 方向映射
        dir_map = {
            'n': '北',
            's': '南',
            'e': '东',
            'w': '西'
        }
        
        # 转换英文方向为中文
        current_dir = dir_map.get(direction.lower(), direction)
        
        # 获取当前房间名
        current_room = self.current_map.get_room_at_position(self.x, self.y)
        if not current_room:
            return False
            
        # 获取当前房间的出口配置
        room_config = self.current_map.rooms.get(current_room)
        if not room_config:
            return False
            
        # 检查是否有对应方向的出口
        exits = room_config.get('exits', {})
        if current_dir not in exits:
            return False
            
        # 获取目标房间名
        target_room = exits[current_dir]
        
        # 更新位置到目标房间
        for pos, room_name in self.current_map.room_positions.items():
            if room_name == target_room:
                self.x, self.y = pos
                return True
                
        return False
    
    def gain_exp(self, amount):
        """获得经验值并处理升级"""
        self.exp += amount
        while self.exp >= self.level * 100:  # 简单的升级条件
            self.level_up()
    
    def level_up(self):
        """升级处理"""
        self.level += 1
        self.exp -= (self.level - 1) * 100
        
        # 属性提升
        self.max_hp += 10
        self.max_mp += 5
        self.hp = self.max_hp
        self.mp = self.max_mp
        
        # 基础属性提升
        self.strength += 2
        self.agility += 2
        self.intelligence += 2
        self.vitality += 2
    
    def take_damage(self, damage):
        """受到伤害"""
        self.hp = max(0, self.hp - damage)
        return self.hp <= 0
    
    def heal(self, amount):
        """恢复生命值"""
        self.hp = min(self.max_hp, self.hp + amount)
    
    def use_mp(self, amount):
        """使用魔法值"""
        if self.mp >= amount:
            self.mp -= amount
            return True
        return False
    
    def add_to_inventory(self, item):
        """添加物品到背包"""
        self.inventory.append(item)
    
    def remove_from_inventory(self, item):
        """从背包中移除物品"""
        if item in self.inventory:
            self.inventory.remove(item)
            return True
        return False
    
    def equip_item(self, item):
        """装备物品"""
        if item.type in self.equipment:
            old_item = self.equipment[item.type]
            if old_item:
                self.add_to_inventory(old_item)
            self.equipment[item.type] = item
            self.remove_from_inventory(item)
            return True
        return False
    
    def learn_skill(self, skill):
        """学习新技能"""
        if skill not in self.skills:
            self.skills.append(skill)
            return True
        return False
